package com.learn.springboot3.controller;


import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("lock")
public class ReEntrantLockController {

    @Autowired
    RedissonClient redissonClient;

    /**
     * 使用redisson测试可重入锁
     * @return
     */
    @GetMapping("test")
    public String test(){


        RLock test = redissonClient.getLock("test");
        boolean b = test.tryLock();

        if (b) {
            System.out.println("获取锁成功");
            methed();

        }


        return "hello";
    }

    public void methed(){
        RLock test = redissonClient.getLock("test");
        boolean b = test.tryLock();
        if (b) {
            System.out.println("获取锁成功");
        }
    }
}
